package 单链表反转;

public class jishengjun {

    public static void main(String[] args) {
        Node firstNode = new Node();
        firstNode.data = "1";
        Node currentNode = firstNode;
        for (int i = 1; i < 20; i++) {
            Node tempNode = new Node();
            tempNode.data = String.valueOf(i+1);
            currentNode.next = tempNode;
            currentNode = tempNode;
        }
        reverse(firstNode);
        //遍历打印node
        while (currentNode != null) {
            System.out.println(currentNode.data);
            currentNode = currentNode.next;
        }
    }

    /***
     * 单链表反转
     * @param firstNode
     */
    private static void reverse(Node firstNode) {
        Node currentNode = firstNode;
        Node preNode = null;
        Node next = null;
        while (currentNode != null) {
            next = currentNode.next;
            currentNode.next = preNode;
            preNode = currentNode;
            currentNode = next;
        }
    }

    static class Node {
        String data;
        Node next;
    }
}
